package com.samsung.sds.test.proxy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;

public class ScriptProxy {
	public static String getRemoteScript(String url) {
		String remoteScriptString = null;
		HttpClient client = new HttpClient();
		GetMethod method = new GetMethod(url);
		try {
			int statusCode = client.executeMethod(method);

			if (statusCode == HttpStatus.SC_OK) {
				StringBuffer buffer = new StringBuffer();
				BufferedReader br = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "utf-8"));
				String line;
				
				while ((line = br.readLine()) != null) {
					buffer.append(line);
				}
				
				remoteScriptString = buffer.toString();
			}
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return remoteScriptString;
	}
}
